package com.yocard.yxyd.adapter;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.yocard.yxyd.utils.util.ActivityUtils;
import com.yocard.yxyd.utils.util.StringUtils;
import com.yocard.yxyd.utils.util.TimeUtils;
import com.yocard.yxyd.R;
import com.yocard.yxyd.http.model.response.TrackListBean;
import com.yocard.yxyd.ui.activity.AddFollwActivity;
import com.yocard.yxyd.utils.CommonUtils;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created on 2019/4/16.
 */
public class CsmRefundAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    final int ITEM_TOP = 0x11;
    final int ITEM_NORMOL = 0x22;
    final int ITEM_BOTTOM = 0x33;
    private List<TrackListBean.OrderTrackEntity> datas;
    private String csmId;
    private int canTrack;

    public CsmRefundAdapter(List<TrackListBean.OrderTrackEntity> datas, String csmId) {
        this.datas = datas;
        this.csmId = csmId;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View view = null;
//        if (i == ITEM_TOP) {
//            view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.item_refund_top, viewGroup, false);
//            return new ViewHolderHead(view);
//        } else {
            view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.item_refund, viewGroup, false);
            return new ViewHolder(view);
//        }
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder viewHolder, int i) {
        if (viewHolder instanceof ViewHolderHead) {
            ViewHolderHead holderHead = (ViewHolderHead) viewHolder;
            holderHead.llAdd.setOnClickListener(view -> {
                Bundle bundle = new Bundle();
                bundle.putString("csmId", csmId);
                ActivityUtils.startActivity(bundle, AddFollwActivity.class);
            });
            if (canTrack == 1) {
                holderHead.llAdd.setEnabled(true);
                holderHead.tvTrack.setEnabled(true);
            } else {
                holderHead.llAdd.setEnabled(false);
                holderHead.tvTrack.setEnabled(false);
            }
        }

        if (viewHolder instanceof ViewHolder) {
            ViewHolder holder = (ViewHolder) viewHolder;
            TrackListBean.OrderTrackEntity trackListBean = datas.get(i);
            holder.tvTime.setText(TimeUtils.millis2String(trackListBean.getCreated_at() * 1000));
            holder.tvStatus.setText(CommonUtils.getStatus(trackListBean.getType()));
            if (StringUtils.isEmpty(trackListBean.getRemark())) {
                holder.tvNote.setText("备注：无");
            } else {
                holder.tvNote.setText("备注：" + trackListBean.getRemark());
            }

            if (datas.size() > 1 && i < datas.size() - 1) {
                holder.divier.setVisibility(View.VISIBLE);
            } else {
                holder.divier.setVisibility(View.GONE);
            }
        }
    }

    @Override
    public int getItemCount() {
        return datas == null ? 0 : datas.size();
    }

//    @Override
//    public int getItemViewType(int position) {
//        if (position == 0) {
//            return ITEM_TOP;
//        } else if (position == getItemCount() - 1) {
//            return ITEM_BOTTOM;
//        } else {
//            return ITEM_NORMOL;
//        }
//    }

    class ViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.tv_time)
        TextView tvTime;
        @BindView(R.id.tv_status)
        TextView tvStatus;
        @BindView(R.id.tv_note)
        TextView tvNote;
        @BindView(R.id.divier)
        View divier;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

    class ViewHolderHead extends RecyclerView.ViewHolder {
        @BindView(R.id.ll_add)
        LinearLayout llAdd;
        @BindView(R.id.tv_track)
        TextView tvTrack;

        public ViewHolderHead(@NonNull View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

    public void setCanTrack(int canTrack) {

        this.canTrack = canTrack;
    }
}
